package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.LogicException;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.DepartmentService;
import cn.wolfcode.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    private DepartmentService departmentService;

    @Autowired
    public void setDepartmentService(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    private EmployeeMapper employeeMapper;
    @Autowired
    public void setEmployeeMapper(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public PageInfo<Employee> listPage(QueryObject queryObject) {
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        List<Employee> employees = employeeMapper.listPage(queryObject);
        return new PageInfo<>(employees);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Employee record) {
        return employeeMapper.insert(record);
    }

    @Override
    public Employee selectByPrimaryKey(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Employee record) {
        return employeeMapper.updateByPrimaryKey(record);
    }

    @Override
    public Employee cheeckLogin(String username, String password) {
        //使用SPringMvc的StringUtils的工具类
        if (!StringUtils.hasText(username)){
            throw  new LogicException("用户名不能为空");
        }
        if (!StringUtils.hasText(password)){
            throw  new LogicException("用户名不能为空");
        }
        Employee employee = employeeMapper.check(username, password);
        if (employee == null){
            throw  new LogicException("用户名或者密码错误");
        }
        if (employee.getStatus() == 0){
            throw  new  LogicException("你的账号被禁用了");
        }
        return  employee;
    }

    @Override
    public int deleteInBatches(Long[] ids) {
        return employeeMapper.deleteInBatches(ids);
    }

    @Override
    public Employee checkUsername(String username) {
        return employeeMapper.selectByUsername(username);
    }

    @Override
    public void updateStatus(int status, Long id) {
        employeeMapper.updateStatus(status, id);
    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        //通过文件的输出流传给poi拿到WorkBook的excel对象
        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
        //通过excel对象拿到第一张表格
        HSSFSheet sheet = workbook.getSheetAt(0);
        //获取最后一行索引
        int lastRowNum = sheet.getLastRowNum();
        //遍历整个表格，然后创建对象插入数据库中
        //注意标题行不读，i不是0开始
        for (int i = 1; i <= lastRowNum; i++) {
            //创建对象
            Employee employee = new Employee();
            //获取当前的行对象
            Row row = sheet.getRow(i);
            //获取用户名,用户名在第0列，所以索引为0
            String username = row.getCell(0).getStringCellValue();
            //判断用户名是否存在
            if ((employeeMapper.selectByUsername(username) )!= null){  //如果用户名不为空，表示找得到，则数据库已经有
                throw  new RuntimeException("用户名已经存在");
            }
            employee.setUsername(username);
            employee.setPassword( row.getCell(1).getStringCellValue());
            employee.setEmail(row.getCell(2).getStringCellValue());
            //由于employee对象的age属性为int类型，而表单的数据为string类型，
            //所以在设值的时候需要转换类型
            employee.setAge((int) row.getCell(3).getNumericCellValue());
            employee.setName(row.getCell(4).getStringCellValue());
            employee.setDeptId((long) row.getCell(5).getNumericCellValue());
             employeeMapper.insert(employee);
        }
    }


}
